package chances.declare.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import chances.declare.entity.CPContent;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class WriteContentExcelService {
    public void exportExcel(String path, List<CPContent> list1,
            Map<String, String> contentTypeResult, Map<String, String> exclusiveTypeResult,
            Map<String, String> statusTypeResult, Map<String, String> isAdoptedTypeResult) {
        WritableWorkbook book = null;
        WritableFont wf = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD,
                false);
        WritableFont title = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.BOLD,
                false);
        WritableCellFormat wcf = new WritableCellFormat(wf);
        WritableCellFormat wcf_title = new WritableCellFormat(title);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            wcf.setWrap(true);// 单元格自动换行
            wcf.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
            wcf_title.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
        } catch (WriteException e1) {
            e1.printStackTrace();
        }
        System.out.println(path);
        String info1[] = { "片名", "录入时间", "申报时间", "上线时间", "分类", "是否独家", "状态", "是否采纳" };
        try {
            book = Workbook.createWorkbook(new File(path));
            // 生成工作表，参数0表示第一页
            WritableSheet sheet1 = book.createSheet("内容申报表", 0);
            sheet1.setColumnView(0, 45);// 设置列宽
            sheet1.setColumnView(1, 30);
            sheet1.setColumnView(2, 30);
            sheet1.setColumnView(3, 30);
            sheet1.setColumnView(4, 10);
            sheet1.setColumnView(5, 10);
            sheet1.setColumnView(6, 10);
            sheet1.setColumnView(7, 10);
            // 表头导航
            for (int j = 0; j < 8; j++) {
                Label label = new Label(j, 0, info1[j], wcf_title);
                sheet1.addCell(label);
            }
            for (int i = 0; i < list1.size(); i++) {
                String createTime = list1.get(i).getCreateTime() == null?"" : sdf.format(list1.get(i).getCreateTime());
                String declareTime = list1.get(i).getDeclareTime() == null?"" : sdf.format(list1.get(i).getDeclareTime());
                String onlineTime = list1.get(i).getOnlineTime() == null?"" : sdf.format(list1.get(i).getOnlineTime());
                sheet1.setRowView(i + 1, 350);// 设置行高
                sheet1.addCell(new Label(0, i + 1, list1.get(i).getName(), wcf));
                sheet1.addCell(new Label(1, i + 1, createTime, wcf));
                sheet1.addCell(new Label(2, i + 1, declareTime, wcf));
                sheet1.addCell(new Label(3, i + 1, onlineTime, wcf));
                sheet1.addCell(new Label(4, i + 1, contentTypeResult.get(list1.get(i).getContentType()), wcf));
                sheet1.addCell(new Label(5, i + 1, exclusiveTypeResult.get(list1.get(i).getExclusive().toString()), wcf));
                sheet1.addCell(new Label(6, i + 1, statusTypeResult.get(list1.get(i).getStatus().toString()), wcf));
                sheet1.addCell(new Label(7, i + 1, isAdoptedTypeResult.get(list1.get(i).getIsAdopted().toString()), wcf));
            }
            // 写入数据并关闭文件
            book.write();
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (book != null) {
                try {
                    book.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
